﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;

namespace QuanLyKTX
{
    using Telerik.WinControls;
    using Telerik.WinControls.UI;
    using DAL;

    public partial class frm_BillExported : Telerik.WinControls.UI.RadForm
    {

        #region Private Methods

        private void DefaultLoadPage()
        {
            for (int i=1; i <= 12; i++)
            {
                RadListDataItem lItem = new RadListDataItem();
                lItem.Font = new Font("Segoe UI", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
                lItem.Value = i;
                lItem.Text = i.ToString();
                radDropDownList_KyThang.Items.Add(lItem);
            }

            radDropDownList_KyThang.Items[0].Selected = true;

            radMaskedEditBox_Nam.Value = DateTime.Now.Year.ToString();

            radGridView_HoaDon.AutoGenerateColumns = false;
        }

        private void BindGridHoaDon(byte kyThang, string nam)
        {
            using (var db = new QuanLyKTXDataContext())
            {
                var results = from p in db.tbl_HoaDonDienNuocs
                              where p.KyThang == kyThang && p.Nam == nam
                              select p;

                if (results.Count() > 0)
                    radGridView_HoaDon.DataSource = results;
                else radGridView_HoaDon.DataSource = null;
            }
        }

        private void BindGridTienPhong()
        {
            using (var db = new QuanLyKTXDataContext())
            {
                
            }
        }

        #endregion

        #region Events

        private void radButton_Thoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void radMaskedEditBox_Nam_ValueChanged(object sender, EventArgs e)
        {
            if (radMaskedEditBox_Nam.Value.ToString() != string.Empty)
            {
                BindGridHoaDon(Convert.ToByte(radDropDownList_KyThang.SelectedValue.ToString()), radMaskedEditBox_Nam.Value.ToString());
            }
            else
            {                
                radMaskedEditBox_Nam.Focus();
            }
        }

        private void radDropDownList_KyThang_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            BindGridHoaDon(Convert.ToByte(radDropDownList_KyThang.SelectedValue.ToString()), radMaskedEditBox_Nam.Value.ToString());
        }

        private void radButton_In_Click(object sender, EventArgs e)
        {
            if (radGridView_HoaDon.Rows.Count() > 0)
            {
                GridViewSelectedRowsCollection rows = radGridView_HoaDon.SelectedRows;
                List<tbl_HoaDonDienNuoc> lHoaDonDienNuoc = new List<tbl_HoaDonDienNuoc>();

                foreach (var row in rows)
                {
                    using (var db = new QuanLyKTXDataContext())
                    {
                        var result = (from p in db.tbl_HoaDonDienNuocs
                                      where p.MaHoaDon == new Guid(row.Cells[0].Value.ToString())
                                      select p).FirstOrDefault();

                        lHoaDonDienNuoc.Add(result);
                    }
                }

                frm_ReportEWBill report = new frm_ReportEWBill(lHoaDonDienNuoc);
                report.ShowDialog();
            }
        }

        private void frm_BillExported_Activated(object sender, EventArgs e)
        {
            DefaultLoadPage();
            BindGridHoaDon(Convert.ToByte(radDropDownList_KyThang.SelectedValue.ToString()), radMaskedEditBox_Nam.Value.ToString());
        }

        private void frm_BillExported_Load(object sender, EventArgs e)
        {

        }

        private void radButton_Thoát_Click(object sender, EventArgs e)
        {
            Close();
        }

        public frm_BillExported()
        {
            InitializeComponent();
        }
        #endregion

        private void radButton_PThoat_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
